package com.pi.pro.xcmg.am.was.edi.sap.service;





import com.pi.common.utils.StringUtils;
import com.pi.common.utils.TextUtils;
import com.pi.config.SapWmsClientSecretConfig;
import com.pi.core.exchange.vo.NamedServiceProxy;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.edi.sap.common.SapWasXcmgExchangeSupport;
import com.pi.pro.xcmg.am.was.edi.sap.dto.reply.DeliveryNoteData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.reply.DeliveryNoteReplyData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.reply.DeliveryNoteTransReplyData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.req.DeliveryNoteQueryData;
import com.pi.pro.xcmg.am.was.edi.sap.dto.req.DeliveryNoteTransData;
import com.pi.wms.fac.conn.HttpAccessUrl;
import com.pi.wms.fac.conn.HttpConstants;
import com.pi.wms.fac.conn.HttpclientProxy;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;
import java.util.*;


/**
 */
@Getter
@Setter
public class DeliveryNoteServiceProxy implements NamedServiceProxy {

    private String category;
    private String code;

    private HttpclientProxy sapDeliveryClient;
    private HttpAccessUrl sapDeliveryLoadPostAccessUrl;
    private HttpAccessUrl sapDeliveryTransPostAccessUrl;

    @Autowired
    private SapWmsClientSecretConfig secretConfig;

    //
    public DeliveryNoteReplyData load(DeliveryNoteQueryData queryData) {
        DeliveryNoteReplyData replyData= SapWasXcmgExchangeSupport.blankOk();
        String replyText = "";
        if (this.sapDeliveryClient==null){
            return replyData;
        }
        Map<String,String> headers = new HashMap<>();
        String authinfo=this.encodeAuthParams();
        try{
            headers.put("Content-Type", XcmgAmConstants.HTTP_CONTENT_TYPE_HTTP_JSON);
            //headers.put("Username", secretConfig.getAppcode());
            //headers.put("Password", secretConfig.getSecretkey());
            headers.put("Authorization", " Basic " + authinfo);  // 设置基本认证头

            replyText = this.sapDeliveryClient.post(this.sapDeliveryLoadPostAccessUrl, null, headers,
                    HttpConstants.URL_METHOD_POST, TextUtils.toJsonText(queryData));

            System.out.println("#####################SAP-DEPLIVERYNOTE-REPLY:"+replyText);

            replyData= TextUtils.toObject(replyText, DeliveryNoteReplyData.class);
        }catch (Exception e){
            e.printStackTrace();
            replyText="send task report error.//"+e.getMessage();
        }
        return replyData;
    }

    public DeliveryNoteTransReplyData upload(List<DeliveryNoteTransData> transDatas){
        DeliveryNoteTransReplyData replyData=null;
        String replyText = "";
        if (this.sapDeliveryClient==null){
            return replyData;
        }
        Map<String,String> headers = new HashMap<>();
        String authinfo=this.encodeAuthParams();
        try{
            headers.put("Content-Type", XcmgAmConstants.HTTP_CONTENT_TYPE_HTTP_JSON);
            //headers.put("Username", secretConfig.getAppcode());
            //headers.put("Password", secretConfig.getSecretkey());
            headers.put("Authorization", "Basic " + authinfo);  // 设置基本认证头

            replyText = this.sapDeliveryClient.post(this.sapDeliveryTransPostAccessUrl, null, headers,
                    HttpConstants.URL_METHOD_POST, TextUtils.toListJsonText(transDatas));

            System.out.println("#####################SAP-DEPLIVERYNOTE-REPLY:"+replyText);

            replyData= TextUtils.toObject(replyText, DeliveryNoteTransReplyData.class);
        }catch (Exception e){
            e.printStackTrace();
            replyText="send task report error.//"+e.getMessage();
        }
        return replyData;
    }

    private String encodeAuthParams(){
        String authString = this.secretConfig.getAppcode() + ":" + secretConfig.getSecretkey();
        return Base64.getEncoder().encodeToString(authString.getBytes(StandardCharsets.UTF_8));
    }

}
